var clamp = function (num, min, max) { return Math.min(Math.max(num, min), max); };
var LinearScroller = /** @class */ (function () {
    function LinearScroller(viewSize, contentSize) {
        this._velocity = 0;
        this._velocityBase = 0;
        this._offset = 0;
        this._tapped = false;
        this._val = 0;
        this._scrolling = false;
        this.viewSize = viewSize;
        this.contentSize = contentSize;
        this._limit = viewSize - contentSize;
    }
    // 点击的时候传递点击位置
    LinearScroller.prototype.start = function (v) {
        this._tapped = true;
        this._val = v;
        this._velocityBase = 0;
        this._scrolling = false;
    };
    // 移动的时候传递当前位置
    LinearScroller.prototype.move = function (v) {
        if (!this._tapped) {
            return;
        }
        this._velocityBase = v - this._val;
        this._val = v;
        this._offset += this._velocityBase;
        this._scrolling = true;
        this.normalizeOffset();
    };
    // 停止移动或取消的时候
    LinearScroller.prototype.finish = function () {
        this._tapped = false;
    };
    // dt 表示 delta secs
    LinearScroller.prototype.update = function (dt) {
        if (!this._scrolling || dt <= 0) {
            return;
        }
        // 2 倍 dt，抄的小镇数值，不要问为什么
        dt += dt;
        if (this._tapped) {
            this._velocity = this._velocityBase / dt;
        }
        else {
            // 松开手的情况下，线性递减
            this._velocity = clamp(this._velocity, -LinearScroller.VELOCITY_MAX, LinearScroller.VELOCITY_MAX);
            var deltaVel = LinearScroller.VEL_DELTA_BASE * dt;
            if (deltaVel > Math.abs(this._velocity)) {
                this._velocity = 0;
            }
            else {
                this._velocity += (this._velocity > 0) ? -deltaVel : deltaVel;
            }
            this._offset += this._velocity * dt;
        }
        if (this.normalizeOffset()) {
            this._velocity = 0;
        }
        if (this._velocity === 0) {
            this._scrolling = false;
        }
    };
    Object.defineProperty(LinearScroller.prototype, "offset", {
        get: function () {
            return this._offset;
        },
        set: function (o) {
            this._offset = o;
            this.normalizeOffset();
            this._scrolling = false;
        },
        enumerable: false,
        configurable: true
    });
    LinearScroller.prototype.normalizeOffset = function () {
        if (this._offset > 0) {
            this._offset = 0;
            return true;
        }
        else if (this._offset < this._limit) {
            this._offset = this._limit;
            return true;
        }
        return false;
    };
    LinearScroller.VELOCITY_MAX = 1250;
    LinearScroller.VEL_DELTA_BASE = 500;
    return LinearScroller;
}());
